package com.lanxin.hr.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lanxin.hr.entity.JcBasicdatavalue;
import com.lanxin.hr.service.IJcBasicdatavalueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2025-07-18
 */
@Controller
@RequestMapping("/hr/jc-basicdatavalue")
public class JcBasicdatavalueController {
    @Autowired
    private IJcBasicdatavalueService jcBasicdatavalueService;

    /**
     * 分页列表所有的基本数据
     * */
    @RequestMapping("/listBasicdatavalue")
    public ModelAndView listBasicdatavalue(@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer pageSize,@RequestParam(required = false) Integer type) {
        IPage<JcBasicdatavalue> basicdatavalueIPage=jcBasicdatavalueService.listBasicdatavalue(page,pageSize,type);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("basicdatavalues",basicdatavalueIPage);
        //获取所有的基本数据类型
        JcBasicdatavalue.Type[] basicdatavalueTypes=JcBasicdatavalue.Type.values();
        modelAndView.addObject("currentType",type);
        modelAndView.addObject("basicdatavalueTypes",basicdatavalueTypes);
        modelAndView.setViewName("hr/basicdata/listBasicdata");
        return modelAndView;
    }

    /**
     * 跳转添加基本数据页面
     * */
    @RequestMapping("/toAddBasicdata")
    public ModelAndView toAddBasicdata() {
        ModelAndView modelAndView=new ModelAndView();
        //获取所有的基本数据类型
        JcBasicdatavalue.Type[] basicdatavalueTypes=JcBasicdatavalue.Type.values();
        modelAndView.addObject("basicdatavalueTypes",basicdatavalueTypes);
        modelAndView.setViewName("hr/basicdata/addBasicData");
        return modelAndView;
    }

    /**
     * 添加基本数据
     * */
    @RequestMapping("/addBasicdata")
    public String addBasicdata(JcBasicdatavalue basicdatavalue) {
        jcBasicdatavalueService.addBasicdatavalue(basicdatavalue);
        return "redirect:listBasicdatavalue";
    }

    /**
     * 检测添加的基本数据是否已经存在
     * */
    @RequestMapping("/checkBasicdataName")
    @ResponseBody
    public String checkBasicdataName(@RequestParam String name) {
        boolean flag=jcBasicdatavalueService.checkBasicdataName(name);
        ObjectMapper objectMapper=new ObjectMapper();
        HashMap<String,Boolean> map=new HashMap<>();
        map.put("valid",flag);
        String json=null;
        try {
            json=objectMapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            json="{\"valid\":false}";
            throw new RuntimeException(e);
        }
        return json;
    }

    /**
     * 前往修改页面
     * */
    @RequestMapping("/toUpdateBasicdata")
    public ModelAndView toUpdateBasicdata(@RequestParam Integer oid) {
        ModelAndView modelAndView=new ModelAndView();
        JcBasicdatavalue basicdatavalue= jcBasicdatavalueService.getBasicdatavalueById(oid);
        modelAndView.addObject("basicdatavalue",basicdatavalue);
        modelAndView.setViewName("hr/basicdata/updateBasicData");
        return modelAndView;
    }

    /**
     * 对基本数据的修改进行验证
     * */
    @RequestMapping("/checkUpdateBasicdata")
    @ResponseBody
    public String checkUpdateBasicdata(JcBasicdatavalue basicdatavalue) {
        boolean s=jcBasicdatavalueService.checkUpdateBasicdata(basicdatavalue);
        ObjectMapper objectMapper=new ObjectMapper();
        HashMap<String,Boolean> map=new HashMap<>();
        map.put("valid",s);
        String json=null;
        try {
            json= objectMapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return json;
    }

    /**
     * 修改基本数据
     * */
    @RequestMapping("/updateBasicdata")
    public String updateBasicdata(JcBasicdatavalue basicdatavalue) {
        jcBasicdatavalueService.updateBasicdatavalue(basicdatavalue);
        return "redirect:listBasicdatavalue";
    }

    /**
     * 删除基本数据
     * */
    @RequestMapping("/deleteBasicdata")
    @ResponseBody
    public String deleteBasicdata(@RequestParam(required = false) Integer oid) {
        boolean s=jcBasicdatavalueService.deleteBasicdatavalue(oid);
        ObjectMapper objectMapper=new ObjectMapper();
        String json=null;
        try {
            json=objectMapper.writeValueAsString(s);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        System.out.println(json);
        return json;
    }
}
